package com.yjck.jwt.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.yjck.jwt.config.JwtConstants;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author EDZ
 */
public class JwtUtil {

//    public static String jwtKey = "xJgHgsG84io3iN%WWRDe4qfq";

    /**
     * 生成token
     *
     * @return
     */
    public static String createJwt(String userId, String type) {

        String jwtKey = JwtConstants.APP.equals(type) ? JwtConstants.APP_JWT_TOKEN : JwtConstants.WEB_JWT_TOKEN;

        try {
            // 设置过期时间
            Date date = new Date(System.currentTimeMillis() + 1000 * 60);
            // 私钥和加密算法
            Algorithm algorithm = Algorithm.HMAC256(jwtKey);
            // 设置头部信息
            Map<String, Object> header = new HashMap<>(2);
            header.put("Type", "Jwt");
            header.put("alg", "HS256");
            // 返回token字符串
            String token = JWT.create()
                    .withHeader(header)
                    .withAudience(userId)
                    .withExpiresAt(date)
                    .sign(algorithm);
            return token;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 检验token是否正确
     *
     * @param token 需要校验的token
     * @return 校验是否成功
     */
    public static boolean verify(String token, String type) {

        String jwtKey = JwtConstants.APP.equals(type) ? JwtConstants.APP_JWT_TOKEN : JwtConstants.WEB_JWT_TOKEN;

        try {
            //设置签名的加密算法：HMAC256
            Algorithm algorithm = Algorithm.HMAC256(jwtKey);
            JWTVerifier verifier = JWT.require(algorithm).build();
            verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 解析token
     *
     * @param token
     */
    public static String pareJwt(String token, String type) {

        String jwtKey = JwtConstants.APP.equals(type) ? JwtConstants.APP_JWT_TOKEN : JwtConstants.WEB_JWT_TOKEN;
        try {
            Algorithm algorithm = Algorithm.HMAC256(jwtKey);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            String userId = jwt.getAudience().get(0);
//            String loginName = jwt.getClaim("loginName").asString();
            return userId;
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String[] args) {
//        String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTEiLCJpYXQiOjE2MTYzODA2NTQsInJvbGVzIjoiYWRtaW4iLCJzdWIiOiLlsI_nmb0iLCJleHAiOjE2MTYzODA3MTR9.A6tHc_4QsgdXoVuV7KMNf4cpEQtYqn5N-N6WhfFhc8U";
        String token = JwtUtil.createJwt("1111111", JwtConstants.APP_JWT_TOKEN);
//        System.out.println(token);
        boolean verify = JwtUtil.verify(token, JwtConstants.APP_JWT_TOKEN);
        System.out.println(verify);
        String s = JWT.decode(token).getAudience().get(0);
        System.out.println(s);
    }

}
